﻿using System;
using System.Drawing;

namespace ClockwiseClient
{
    public class ClassGraphDapan : ClassGraph
    {
        const int widthTitle = 256;
        int mode = 0;

        public override void Paint(Graphics g, Rectangle rect)
        {
            this.rect = rect;
            if (format.LineAlignment != StringAlignment.Center) format.LineAlignment = StringAlignment.Center;
            if (format.Alignment != StringAlignment.Center) format.Alignment = StringAlignment.Center;
            String tips = "显示区域太小了";
            stringSize = g.MeasureString(tips, font);
            if (rect.Width < 640 || rect.Height < 480)
            {
                if (stringSize.Width > rect.Width)
                {
                    tips = "太小了";
                    stringSize = g.MeasureString(tips, font);
                }
                if (stringSize.Width > rect.Width || stringSize.Height > rect.Height) return;
                PointF pointF = new PointF(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2 + stringSize.Height / 10);
                g.DrawString(tips, font, Brushes.Cyan, pointF, format);
                return;
            }
            ShowDapan(g, rect);
        }

        public override EnumGraph GetGraphType() { return EnumGraph.GRAPH_DAPAN; }

        public override void SetGraphMode(int mode)
        {
            this.mode = mode;
        }

        public override void MouseWheel(int delta)
        {
        }

        void ShowDapan(Graphics g, Rectangle rect)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            string text;
            SizeF textSize;
            PointF pointF;
            ClassStockItem si = classStockData.GetClassIndexItem(mode);
            if (si != null)
            {
                text = si.code;
                textSize = g.MeasureString(text, fontTitle2);
                format.Alignment = StringAlignment.Near;
                int left = rect.Right - widthTitle;
                pointF = new PointF(left, rect.Top + textSize.Height / 2 + textSize.Height / 10);
                g.DrawString(text, fontTitle2, Brushes.Cyan, pointF, format);
                left += (int)textSize.Width;
                text = si.name;
                textSize = g.MeasureString(text, fontTitle2);
                pointF = new PointF(left, rect.Top + textSize.Height / 2 + textSize.Height / 10);
                g.DrawString(text, fontTitle2, Brushes.Yellow, pointF, format);
                Pen pen = new Pen(Color.Red, 1);
                left = rect.Right - widthTitle - 16;
                int row = (int)textSize.Height + 8;
                g.DrawLine(pen, left, row, rect.Right, row);
                g.DrawLine(pen, left, row, left, rect.Bottom);
            }
        }
    }
}
